#input thread_id,and view its posts

import wsgiref.handlers

from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from BeautifulSoup import BeautifulSoup,NavigableString,Tag

from xmw import tianya
import xmw
from xmw.db import ds
from xmw import error
import os
import urllib2, urllib
import uuid
import __main__
from appengine_utilities import sessions
from google.appengine.ext import webapp
import base64
import zlib
import logging
import datetime
import string
import simplejson
from google.appengine.ext import db
class Result:
    def __init__(self):
        self.body=None
        self.title=None
        self.url    =None
class URL(db.Model):
    u=db.URLProperty(required = True)           
class MainPage(xmw.RequestHandler):
    
    def tag_to_Str(self,tag):
        text=""
        for c in tag.contents:
            text=text+str(c)
        return text    
    
    def test_url(self,url):
        url=string.strip(url)
        if url.upper().find("CONTENT") <0:
            d=ds.IllegalURL(url=url)
            q=ds.IllegalURL.all()
            q.filter('url = ', url)
            u=q.get()
            if u is None:
                d.put()
            logging.log( logging.INFO, "illegal URl:%s"% url)
            return False
        else:
            return True
                  
    def get(self):
        self.sess = sessions.Session()
        self.cookie_sess = sessions.Session(writer="cookie") 
        
        session_user =None
        if self.sess.has_key("user"):
            session_user=self.sess["user"]
        logging.log( logging.INFO, "session_user :%s"%str(session_user))
        
        template_values ={"session_user":session_user}
        
        if not self.request.GET.has_key('q'):
            self.render_template(template_values, '../html/douban_search.html')
            return
        
        
        q=self.request.GET['q']
        q=q.strip()
        is_url=True
        is_ok=False
        try:
            url=URL(u=q)
            is_ok=self.test_url(q)
        except:
            is_url=False
        if is_url:
            logging.log( logging.INFO, "Searching URl:%s"% str(url))

        
        if not self.sess.has_key("user"):
            if self.sess.has_key('login_action'):
                login_action    =   self.sess['login_action']
            else:
                login_action=ds.Login_Action()
                self.sess['login_action']   =login_action
                
            login_action.action ="/search"
            login_action.params="q="+urllib.quote(q.encode('utf-8'))
            login_action.created=datetime.datetime.now()
            login_action.put()
                
        if len(q) == 0:
            self.render_template(template_values, '../html/douban_search.html')
            return
        search_key_word=q
        
        # rewrite the search part with google search ajax 2010 1 23
        
        """
        q=q+" site:www.tianya.cn"
        q=urllib.quote(q.encode("GB18030"))


        url = 'http://www.baidu.com/s?wd=%s'%q
        #request = urllib2.Request(url, None, {'Referer': 'www.tianya.cn'})
        logging.log( logging.INFO, "url: %s"% (url))
        page = urllib2.urlopen(url)
        #page = urllib2.urlopen(url)
        soup = BeautifulSoup(page, fromEncoding="GB18030")
        #soup = BeautifulSoup(page)
        page.close()
 
        lst_result=[]
        try:
            for i in range(1,11):
                t=soup.find('table',{"id":str(i)})
                c=t.tr.td
                cc=c.contents
            
                title=cc[0]
                
                url=title["href"]
                title=self.tag_to_Str(title)
                body=cc[2]
                a=body.find('a')
                a.extract()

                text=self.tag_to_Str(body)
                #body.insert(-1,tag)

                result=Result()
                result.body=text
  
                result.url=url
                if not self.test_url(url):
                    continue
                    
                result.title    =title
                #result.q_title=urllib.quote(title)
                
                result.q_title=self.xmw_encoding(title)
                result.q_url=self.xmw_encoding(url)
                #result.q_title=title

                lst_result.append(result)
        except:
            import traceback
            traceback.print_exc()
            lst_result=[]
            lst_result.append("sorry,fail to find posts!")
        """
        #if is_url:
        #    query = urllib.urlencode({'q' : search_key_word.encode("utf-8"),'rsz':'large',})
        #else:
        
        if is_url and is_ok:
            logging.info('directly visit url:%s'%q)
            page=tianya.TianyaPage(url=q,thread_id="NA",offset=-1)
            page.reload()
            page.reset_page_type()
            
            search_key_word=search_key_word.encode('utf-8')
            q_search_key_word=self.xmw_encoding(urllib.quote(search_key_word))
            r=Result()
            r.url=q
            r.title=page.get_title()
            r.body='---'
            r.q_title=self.xmw_encoding(r.title.encode('utf-8'))
            r.q_url=self.xmw_encoding(r.url)
            #lst_result.append(result)
            template_values2 = {
                           "lst_result":[r],
                           "search_key_word":search_key_word,
                           "q_search_key_word":q_search_key_word,
                           "post_count":1,
                           #"login_action":self.xmw_encoding("/search?q=%s"%search_key_word),
                           
                           }
            template_values.update(template_values2)  
        
            self.render_template(template_values, '../html/douban_search.html')
                        
            #self.render_template(None, "../html/douban_search")
            return
        
        query = urllib.urlencode({'q' : search_key_word.encode("utf-8")+' site:www.tianya.cn','rsz':'large',})

        url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&%s ' \
        % (query)
        search_results = urllib.urlopen(url)

        reply=search_results.read()
        #print reply
        json = simplejson.loads(reply)
        results = json['responseData']['results']
        index=0
        lst_result=[]
        for i in results:
            index=index+1
            #print str(index)+":--"
            result=Result()
            url=i['url']
            
            if not self.test_url(url):
                    continue
            result.url=url
            result.title=i['title']
            result.body=i['content']
            result.q_title=self.xmw_encoding(result.title.encode('utf-8'))
            result.q_url=self.xmw_encoding(result.url)
            lst_result.append(result)
            
            #for k in i:
            #    print k+":"+i[k]
        
        search_key_word=search_key_word.encode('utf-8')
        q_search_key_word=self.xmw_encoding(urllib.quote(search_key_word))
        template_values2 = {
                           "lst_result":lst_result,
                           "search_key_word":search_key_word,
                           "q_search_key_word":q_search_key_word,
                           "post_count":len(lst_result),
                           #"login_action":self.xmw_encoding("/search?q=%s"%search_key_word),
                           
                           }  
        template_values.update(template_values2)  
        
        self.render_template(template_values, '../html/douban_search.html')
                        
        #self.render_template(None, "../html/douban_search")
        return

def main():
    application = webapp.WSGIApplication(
                                       [('/search', MainPage)],
                                       debug=True)
    wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()
